﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.Dinasty.BO;

namespace GMS.BasicControls
{
    public partial class QuantityStrips : BackGroundControl
    {
        public QuantityStrips()
        {
            InitializeComponent();
        }
        private DataTable _sourceTable;
        public DataTable SourceTable
        {
            get { return _sourceTable; }
            set { _sourceTable = value; }
        }
        private DataTable _tableSource = new DataTable();

        void CalculateStrips()
        { 
            
            foreach (DataRow dr in _sourceTable.Rows )
            {
                Good good = new Good(dr["CaptionGood"].ToString());
                if (good.GroupGood == 2)
                {
                    DataRow sdr = _tableSource.NewRow();
                    sdr["Suffusion"] = good.Suffusion.ToString();
                    sdr["Color"] = good.Color.ToString();
                    sdr["Lenght"] = good.Lenght.ToString();
                    int i = 0;
                    foreach (string s in good.Scans)
                    {
                        i = i + int.Parse(s);
                    }
                    sdr["Scans"] = i.ToString();
                    _tableSource.Rows.Add(sdr);
                }
            }

            var query = _tableSource.AsEnumerable()
                 .GroupBy(x => new { Suffusion = x["Suffusion"], Color = x["Color"], Lenght = x["Lenght"] })
                 .Select(group => new { Suffusion = group.Key.Suffusion, Color = group.Key.Color, Lenght = group.Key.Lenght, Scans = group.Sum(s => s.Field<int>("Scans")),  Quantity = group.Sum(g => g.Field<int>("Scans"))/1250.0});

            //var group = _tableSource.AsEnumerable().GroupBy(d => d["mc"]).Select(d => new { Value = d.Key, Sum = d.Sum(s => s.Field<double>("summa")) });
            //dataGridView1.DataSource = _tableSource;
            dataGridView1.DataSource = query.ToList();
        }
        void CreateSourceTable()
        {
            DataColumn dc = new DataColumn("Suffusion");
            _tableSource.Columns.Add(dc);
            dc = new DataColumn("Color");
            _tableSource.Columns.Add(dc);
            dc = new DataColumn("Lenght");
            _tableSource.Columns.Add(dc);
            dc = new DataColumn("Scans", Type.GetType("System.Int32"));
            _tableSource.Columns.Add(dc);
            dc = new DataColumn("quantityScans", Type.GetType("System.Decimal"));
            dc.Expression = "Scans / 1250";
            _tableSource.Columns.Add(dc);
        }

        private void QuantityStrips_Load(object sender, EventArgs e)
        {
            CreateSourceTable();
            CalculateStrips();
        }
    }
}
